package com.szgr.baoyin.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.ColorGridAdapter;
import com.szgr.baoyin.adapter.SettingGridAdapter;
import com.szgr.baoyin.adapter.TypeGridAdapter;
import com.szgr.baoyin.model.ProductColor;
import com.szgr.baoyin.model.ProductSetting;
import com.szgr.baoyin.model.ProductType;
import com.szgr.baoyin.myview.NoScrollGridVIew;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择商品颜色和分类的页面
 */
public class ChooseSettingActivity extends BaseActivity implements AdapterView.OnItemClickListener, Animation.AnimationListener {
    private List<ProductType> typeData;
    private List<ProductColor> colorData;
    private List<ProductSetting> settingData;
    private List<ProductType> typeChoose;
    private List<ProductColor> colorChoose;
    private List<ProductSetting> settingChoose;
    private TypeGridAdapter typeAdapter;
    private ColorGridAdapter colorAdapter;
    private SettingGridAdapter settingAdapter;
    private LinearLayout ll_showMsg;
    private TypeGridAdapter typeChooseAdapter;
    private ColorGridAdapter colorChooseAdapter;
    private SettingGridAdapter settingChooseAdapter;
    private Animation cartAnim;
    //购物车里商品数量
    private int productNum = 0;
    private ImageView animCar;
    private TextView btn_add;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_choose_setting);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("机床商城");
        actionBarRight.setImageResource(R.drawable.car);
        actionBarRight.setVisibility(View.VISIBLE);
    }

    @Override
    public void initView() {
        typeChoose = new ArrayList<>();
        colorChoose = new ArrayList<>();
        settingChoose = new ArrayList<>();
        typeData = new ArrayList<>();
        colorData = new ArrayList<>();
        settingData = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            typeData.add(new ProductType());
            colorData.add(new ProductColor());
        }
        for (int i = 0; i < 3; i++) {
            settingData.add(new ProductSetting());
        }
        NoScrollGridVIew nsg_chooseType = (NoScrollGridVIew) findViewById(R.id.choose_setting_type_grid);
        typeAdapter = new TypeGridAdapter(this, typeData);
        nsg_chooseType.setAdapter(typeAdapter);
        nsg_chooseType.setOnItemClickListener(this);

        NoScrollGridVIew nsg_chooseColor = (NoScrollGridVIew) findViewById(R.id.choose_setting_color_grid);
        colorAdapter = new ColorGridAdapter(this, colorData);
        nsg_chooseColor.setAdapter(colorAdapter);
        nsg_chooseColor.setOnItemClickListener(this);

        NoScrollGridVIew nsg_chooseSetting = (NoScrollGridVIew) findViewById(R.id.choose_setting_setting_grid);
        settingAdapter = new SettingGridAdapter(this, settingData);
        nsg_chooseSetting.setAdapter(settingAdapter);
        nsg_chooseSetting.setOnItemClickListener(this);

        NoScrollGridVIew nsg_type = (NoScrollGridVIew) findViewById(R.id.type_grid_view);
        typeChooseAdapter = new TypeGridAdapter(this, typeChoose);
        nsg_type.setAdapter(typeChooseAdapter);
        NoScrollGridVIew nsg_color = (NoScrollGridVIew) findViewById(R.id.color_grid_view);
        colorChooseAdapter = new ColorGridAdapter(this, colorChoose);
        nsg_color.setAdapter(colorChooseAdapter);
        NoScrollGridVIew nsg_setting = (NoScrollGridVIew) findViewById(R.id.setting_grid_view);
        settingChooseAdapter = new SettingGridAdapter(this, settingChoose);
        nsg_setting.setAdapter(settingChooseAdapter);

        ll_showMsg = (LinearLayout) findViewById(R.id.choose_setting_show_detail);

        findViewById(R.id.choose_setting_buy).setOnClickListener(this);
        findViewById(R.id.choose_setting_connect).setOnClickListener(this);
        btn_add = (TextView) findViewById(R.id.choose_setting_add_car);
        btn_add.setOnClickListener(this);
        animCar = (ImageView) findViewById(R.id.choose_setting_car);
        cartAnim = AnimationUtils.loadAnimation(this, R.anim.cart_anim);
        cartAnim.setAnimationListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.choose_setting_buy:

                break;
            case R.id.choose_setting_connect:

                break;
            case R.id.choose_setting_add_car:
                animCar.startAnimation(cartAnim);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.choose_setting_type_grid:
                typeChoose.clear();

                for (int i = 0; i < parent.getCount(); i++) {
                    ProductType item = (ProductType) parent.getAdapter().getItem(i);
                    item.setIsSelected(false);
                }
                ProductType item = (ProductType) parent.getAdapter().getItem(position);
                item.setIsSelected(true);
                typeChoose.add(item);

                typeAdapter.notifyDataSetChanged();
                break;
            case R.id.choose_setting_color_grid:
                colorChoose.clear();
                for (int i = 0; i < parent.getCount(); i++) {
                    ProductColor pc = (ProductColor) parent.getAdapter().getItem(i);
                    pc.setIsSelected(false);
                }
                ProductColor productColor = (ProductColor) parent.getAdapter().getItem(position);
                productColor.setIsSelected(true);
                colorChoose.add(productColor);
                colorAdapter.notifyDataSetChanged();
                break;
            case R.id.choose_setting_setting_grid:
                settingChoose.clear();
                for (int i = 0; i < parent.getCount(); i++) {
                    ProductSetting pc = (ProductSetting) parent.getAdapter().getItem(i);
                    pc.setIsSelected(false);
                }
                ProductSetting ps = (ProductSetting) parent.getAdapter().getItem(position);
                ps.setIsSelected(true);
                settingChoose.add(ps);
                settingAdapter.notifyDataSetChanged();
                break;
        }
        showBottom();
    }

    private void showBottom() {
        if (typeChoose.size() > 0 && colorChoose.size() > 0 && settingChoose.size() > 0) {
            typeChooseAdapter.notifyDataSetChanged();
            colorChooseAdapter.notifyDataSetChanged();
            settingChooseAdapter.notifyDataSetChanged();
            ll_showMsg.setVisibility(View.VISIBLE);
        } else {
            ll_showMsg.setVisibility(View.GONE);
        }
    }

    /*****************************anim Listener*************************************/
    @Override
    public void onAnimationStart(Animation animation) {
        animCar.setVisibility(View.VISIBLE);
        btn_add.setEnabled(false);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        animCar.setVisibility(View.GONE);
        actionBarCount.setVisibility(View.VISIBLE);
        productNum ++ ;
        actionBarCount.setText(productNum + "");
        btn_add.setEnabled(true);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
    /******************************************************************/
}
